const MyPromise = require('./4-MyPromise.js');

/**
 * 测试模块1：
 *  1.三种状态改变，成功或失败后状态不会再改变
 *  2.异步改变状态后执行then回调
 *  3.实例化传入的函数执行出错后reject
 *  4.同一个promise的then方法可以多次调用
 */
// const p1 = new MyPromise((resolve, reject) => {
//     // resolve('成功');
//     // reject('失败');
//     setTimeout(() => {
//         // resolve('成功');
//         reject('失败');
//     }, 1000)
//     // throw(new Error('executor error'))
// })

// p1.then(value => {
//     console.log('p1.then value', value)
// }, reason => {
//     console.log('p1.then reason', reason)
// })

// p1.then(value => {
//     console.log('另一个p1.then value', value)
// }, reason => {
//     console.log('另一个p1.then reason', reason)
// })

/**
 * 测试模块2：
 * 1.then方法链式调用，返回普通值、promise、自身的处理，then方法默认参数
 * 2.catch方法
 * 3.finally方法
 * 4.done方法
 */

// const p2 = new MyPromise((resolve, reject) => {
//     // resolve('成功');
//     reject('失败');
// })
// const q1 = new MyPromise((resolve, reject) => {
//     resolve('另一个成功')
// })

// const q2 = p2.then(value => {
//     console.log("p2.then value", value)
//     return '第一个then resolve的返回值'
// }, reason => {
//     console.log("p2.then reason", reason)
//     return '第一个then reject的返回值'
// }).then(value => {
//     console.log('p2.then second value', value)
//     return q1
// }, reason => {
//     console.log('p2.then second reason', reason)
// }).then(value => {
//     console.log('p2.then third value', value)
//     return q2;
// }, reason => {
//     console.log('p2.then third reason', reason)
// })

// q2.then(value => {
//     console.log('q2.then third', value)
//     return q2;
// }).then().then()
// .catch(reason => {
//     console.log('q2.catch third', reason.message)
//     return 'q2.catch before finally的返回值'
// }).finally(() => {
//     console.log('q2.finally')
//     throw new TypeError('TypeError in finally')
//     // return new MyPromise((resolve) => {
//     //     setTimeout(() => {
//     //         console.log('q2.finally------------')
//     //         resolve('q2.finally 返回 promise')
//     //     }, 4000);
//     // })
// }).then(value => {
//     console.log('q2.then after finally', value)
// }).catch(reason => {
//     console.log('q2.catch after finally', reason.message)
//     throw new Error('last Error in catch')
// }).done()



/**
 * 测试模块3：
 *  1.静态方法resolve/reject
 *  2.静态方法all/race
 */

let p3 = new MyPromise((resolve, reject) => {
    resolve('p3 成功')
})
let p4 = new MyPromise((resolve, reject) => {
    // resolve('p3')
    reject('p4 失败')
})

MyPromise.resolve(123).then(result => {
    console.log("MyPromise.resolve(123)", result)
})

MyPromise.resolve(p3).then(result => {
    console.log("MyPromise.resolve(p3)", result)
}).catch(reason => {
    console.log("MyPromise.resolve(p3) catch", reason)
})

MyPromise.reject(123).then(result => {
    console.log("MyPromise.reject(123)", result)
}).catch(reason => {
    console.log("MyPromise.reject(123) catch", reason)
})

MyPromise.reject(p4)
    .then(result => {
        console.log("MyPromise.reject(p4) then", result)
    })
    .catch(reason => {
        console.log("MyPromise.reject(p4) catch", reason)
    })

MyPromise.all([1, 2, p3, p4])
    .then(result => {
        console.log("MyPromise.all([1, 2, p3, p4])", result)
    }, reason => {
        console.log("MyPromise.all([1, 2, p3, p4])", reason)
    })

MyPromise.all('123')
    .then(result => {
        console.log("MyPromise.all('123')", result)
    }, reason => {
        console.log("MyPromise.all('123')", reason)
    })

MyPromise.race([p3, p4]).then(result => {
    console.log("MyPromise.race([p3, p4])", result)
}, reason => {
    console.log("MyPromise.race([p3, p4])", reason)
})




